package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.MeetingMapper;
import com.example.pojo.careerTalk;
import com.example.dto.result;
import com.example.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/meeting")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;

    @Autowired
    private MeetingMapper meetingMapper;

    //分页获取宣讲会信息
    @GetMapping("/talkList")
    public result findAllTalkList(@RequestParam Integer page, @RequestParam Integer size){
        Page<careerTalk> careerTalkPage = new Page<>(page,size);
        IPage<careerTalk> res = meetingService.findAllTalkList(careerTalkPage);
        return result.success(res);
    }

    //根据类型查询
    @GetMapping("/sort")
    public result sortByType(@RequestParam String type,@RequestParam Integer page, @RequestParam Integer size){
        Page<careerTalk> careerTalkPage = new Page<>(page,size);
        IPage<careerTalk> res =  meetingService.sortByType(type,careerTalkPage);
        return result.success(res);
    }

    //根据关键字查询
    @GetMapping("/findByKey")
    public result findByKey(@RequestParam String key,@RequestParam Integer page, @RequestParam Integer size){
        Page<careerTalk> careerTalkPage = new Page<>(page,size);
        IPage<careerTalk> res =  meetingService.findByKey(key,careerTalkPage);
        return result.success(res);
    }

    //叠加查询
    @GetMapping("/MultiFind")
    public result multiFind(@RequestParam String type,@RequestParam String key,@RequestParam Integer page, @RequestParam Integer size){
        Page<careerTalk> careerTalkPage = new Page<>(page,size);
        IPage<careerTalk> res =  meetingService.multiFind(type,key,careerTalkPage);
       return result.success(res);
    }

    //删除宣讲会信息
    @DeleteMapping("/deleteMeeting")
    public result deleteMeeting(@RequestParam String name){
        careerTalk c = meetingMapper.findByDate(name);
        if(c==null){
            System.out.println(name);
            return result.error("删除失败");
        }
        meetingService.deleteMeeting(name);
//      System.out.println();
        return result.success("删除成功");
    }

    //编辑宣讲会信息
    @PostMapping("/editMeeting")
    public result editMeeting(@RequestBody careerTalk c){
        if(c!=null){
            System.out.println(c);
            meetingService.editMeeting(c);
            return result.success("更新成功");
        }
        return result.error("输入内容有误");
    }

    //新增宣讲会信息
    @PostMapping("/addMeeting")
    public result addMeeting(@RequestBody careerTalk c){

        if(c!=null){
            meetingService.addMeeting(c);
            return result.success("新增成功");
        }

        return result.error("输入内容有误");
    }
}
